﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Mystery.SharePoint
{
	/// <summary>
	/// Creates a scope for unsafe updates
	/// </summary>
	public class UnsafeUpdateScope : IDisposable
	{
		SPSite _site;
		SPWeb _web;
		bool _updatingSite = false;

		/// <summary>
		/// Constructs the scope
		/// </summary>
		/// <param name="site">The site to update</param>
		public UnsafeUpdateScope(SPSite site)
		{
			_site = site;
			_web = site.RootWeb;
			_site.AllowUnsafeUpdates = true;
			_web.AllowUnsafeUpdates = true;
			_updatingSite = true;
		}
		/// <summary>
		/// Constructs the scope
		/// </summary>
		/// <param name="web">The web to update</param>
		public UnsafeUpdateScope(SPWeb web)
		{
			_web = web;
			_web.AllowUnsafeUpdates = true;
			_site = web.Site;
		}

		public void Dispose()
		{
			if (_updatingSite == true)
			{
				_site.AllowUnsafeUpdates = false;
				_web.AllowUnsafeUpdates = false;
			}
			else
			{
				_web.AllowUnsafeUpdates = false;
			}
		}
	}
}
